//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.alibaba.nacos.config.server.auth;

import com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.model.User;
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.RowMapperManager;
import com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate;
import com.alibaba.nacos.config.server.service.repository.embedded.EmbeddedStoragePersistServiceImpl;
import com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

@Conditional({ConditionOnEmbeddedStorage.class})
@Component
public class EmbeddedUserPersistServiceImpl implements UserPersistService {
	@Autowired
	private DatabaseOperate databaseOperate;
	@Autowired
	private EmbeddedStoragePersistServiceImpl persistService;

	public EmbeddedUserPersistServiceImpl() {
	}

	public void createUser(String username, String password) {
		String sql = "INSERT INTO users (username, password, enabled) VALUES (?, ?, ?)";

		try {
			EmbeddedStorageContextUtils.addSqlContext(sql, new Object[]{username, password, 1});
			this.databaseOperate.blockUpdate();
		} finally {
			EmbeddedStorageContextUtils.cleanAllContext();
		}

	}

	public void deleteUser(String username) {
		String sql = "DELETE FROM users WHERE username=?";

		try {
			EmbeddedStorageContextUtils.addSqlContext(sql, new Object[]{username});
			this.databaseOperate.blockUpdate();
		} finally {
			EmbeddedStorageContextUtils.cleanAllContext();
		}

	}

	public void updateUserPassword(String username, String password) {
		try {
			EmbeddedStorageContextUtils.addSqlContext("UPDATE users SET password = ? WHERE username=?", new Object[]{password, username});
			this.databaseOperate.blockUpdate();
		} finally {
			EmbeddedStorageContextUtils.cleanAllContext();
		}

	}

	public User findUserByUsername(String username) {
		String sql = "SELECT username,password FROM users WHERE username=? ";
		return (User)this.databaseOperate.queryOne(sql, new Object[]{username}, RowMapperManager.USER_ROW_MAPPER);
	}

	public Page<User> getUsers(int pageNo, int pageSize) {
		PaginationHelper<User> helper = this.persistService.createPaginationHelper();
		String sqlCountRows = "SELECT count(*) FROM users WHERE ";
		String sqlFetchRows = "SELECT username,password FROM users WHERE ";
		String where = " 1=1 ";
		Page<User> pageInfo = helper.fetchPage(sqlCountRows + where, sqlFetchRows + where, (new ArrayList()).toArray(), pageNo, pageSize, RowMapperManager.USER_ROW_MAPPER);
		if (pageInfo == null) {
			pageInfo = new Page();
			pageInfo.setTotalCount(0);
			pageInfo.setPageItems(new ArrayList());
		}

		return pageInfo;
	}

	public List<String> findUserLikeUsername(String username) {
		String sql = "SELECT username FROM users WHERE username LIKE ? ";
		return this.databaseOperate.queryMany(sql, new String[]{"%" + username + "%"}, String.class);
	}
}
